
var imgArray = new Array();

/*添加图片*/
function addImages(url) {
    var tempId = getTimestamp();
    var html = " <div id="+tempId+">";
    if(imgArray.length>0){
        html = html+"<label class=\"col-sm-2 control-label\"></label>"
    }

    html = html + "    <a class=\"fancybox\" href="+url+">" +
               "        <img alt=\"image\" style='width: 15%' src="+url+" />" +
               "    </a>" +
               "    <button class=\"btn btn-sm btn-danger\" img-url="+url+" onclick=\"deleteImg(this,'"+tempId+"')\">" +
               "        <strong>删除</strong>" +
               "    </button>" +
               " </div>";
    //添加页面图片显示
    $('#upload-list').append(html);

    imgArray.push(url);

    console.log(JSON.stringify(imgArray));

}

//删除图片
function deleteImg(obj,tempId) {
    $('#'+tempId).remove();
    var imgUrl = $(obj).attr('img-url');
    imgArray.removeByValue(imgUrl);
    console.log(imgArray);
}

//添加商品
function addGoods() {
    var goods_name = $('#goods_name').val();
    if(checkIsNull(goods_name)){
        alertTips("请填写商品名称！");
        return false;
    }

    var goods_no = $('#goods_no').val();
    if(checkIsNull(goods_no)){
        alertTips("请填写商品编号！");
        return false;
    }

    if(imgArray.length<1){
        alertTips("请上传商品图片！");
        return false;
    }
    var goods_img = imgArray[0];
    var goods_imgs = JSON.stringify(imgArray);

    var goods_unit = $("#goods_unit option:selected").val();
    if(checkIsNull(goods_unit)){
        alertTips("请选择商品单位！");
        return false;
    }

    var goods_brand = $("#goods_brand option:selected").val();
    if(checkIsNull(goods_brand)){
        alertTips("请选择商品品牌！");
        return false;
    }

    var goods_type = $("#goods_type option:selected").val();
    if(checkIsNull(goods_type)){
        alertTips("请选择商品类型！");
        return false;
    }

    var goods_specifications = $("#goods_specifications").val();
    if(checkIsNull(goods_specifications)){
        alertTips("请填写商品规格！");
        return false;
    }

    var desc = $('#desc-content').val();

    var area_price_list = getAreaPriceList();

    var goods = new Goods(goods_no,goods_name,goods_img,goods_imgs,goods_unit,goods_brand,goods_type,goods_specifications,desc,area_price_list);

    console.log(JSON.stringify(goods));
    $.ajax({
        type: "post",
        url: '/goods/addGoods',
        dataType: "json",
        data: {
            'goods_json' : JSON.stringify(goods)
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==='success'){
                alertTips("新增商品成功！");
                location.href='/goods/toGoodsList';
            }else{
                alertTips("新增商品异常！");
            }
        }
    });
}

var chooseCities = new Array();
var priceIds = new Array();

function newCitySelect() {

    var timestamp = getTimestampId();
    priceIds.push(timestamp);
    var html = "<div class=\"hr-line-dashed\"></div>\n" +
        "       <div class=\"form-group\">" +
        "       <label class=\"col-sm-2 control-label\"><font color=\"red\">*</font>指定价格</label>\n" +
        "       <div class=\"col-sm-2\">" +
        "       <input type=\"text\" placeholder=\"例:3.55\" onkeyup=\"this.value=(this.value.match(/\\d+(\\.\\d{0,2})?/)||[''])[0]\" id=\"goods_price_"+timestamp+"\" class=\"form-control\">" +
        "       </div>(单位：元)" +
        "       <div class=\"col-sm-12\" >" +
        "       <label class=\"col-sm-2 control-label\" style=\"margin-right: -17px;margin-left: -8px;\"><font color=\"red\">*</font>区域</label>\n" +
        "       <div style=\"width: 985px;\" id="+timestamp+" >";
    for (var i=0 ;i<cities.length;i++){
        if(i>0){
            html = html+"<label class=\"col-sm-2 control-label\" style=\"margin-right: -17px;margin-left: -8px;\"></label>"
        }
        html = html+"<label class=\"col-sm-1 control-label\">"+cities[i].area_name+"</label>\n" +
        "<label class=\"checkbox-inline \" onclick=\"allCity('"+cities[i].area_name+timestamp+"')\"><font color=\"green\">全部</font></label>";
        for(var k=0 ;k<cities[i].areaList.length;k++){
            if(k===9){
                html = html+"<label class=\"col-sm-2 control-label\" style=\"margin-left: 3px;\"></label>";
            }
            html = html+ "<label class=\"checkbox-inline\" ><input class='check_box_city' type=\"checkbox\" style='margin-top: 1px;' city="+cities[i].area_name+" name="+cities[i].area_name+timestamp+" value="+cities[i].areaList[k].area_name+">"+cities[i].areaList[k].area_name+"</label>"
        }
        html = html+ "<div style=\"margin: 2px;\"></div>"
    }
    html = html+"</div> </div> </div>";
    $('#city_list').append(html);
    removeChooseEven();
    addChooseEven();
}

//选择全部城市
function allCity(name) {
    $("input[name="+name+"]").parent().children('.choose_city_a').each(function(){
        if(!($(this).prop('checked'))){
            $(this).click();
        }
    });
}

//添加选择城市时间
function addChooseEven() {
    $('.check_box_city').addClass("choose_city_a");
    bindChooseEven();
}

//添加选择城市时间
function removeChooseEven() {
    $('.check_box_city').removeClass("choose_city_a");
    $('.check_box_city').unbind();
    // bindChooseEven();
}

//绑定选择城市时间
function bindChooseEven() {
    $('.choose_city_a').click(function () {
        // console.log($(this).attr('class')+"--"+$(this).val()+"---"+$(this).prop('checked'));
        var city = $(this).val();
        var checked = $(this).prop('checked');
        if(checked){
            if(!addChooseCity(city)){
                cancelCheck($(this),city);
            }
        }else{
            removeChooseCity(city);
        }
    });
}

function cancelCheck(obj,city) {
    $(obj).attr('checked',false);
}


//添加已经选择过的区域
function addChooseCity(city) {
    if(!checkExist(chooseCities,city)){
        chooseCities.push(city);
    }else{
        return false;
    }
    console.log(JSON.stringify(chooseCities));
    return true;
}

//删除已经选择过的区域
function removeChooseCity(city) {
    chooseCities.removeByValue(city);
    console.log(JSON.stringify(chooseCities));
}

//获取当前id下的所有区域
function getSelectedArea(priceId) {
    var areaList = new Array();
    console.log(priceId);
    $('#'+priceId).children('.checkbox-inline').each(function () {
        if($(this).children('.check_box_city').prop('checked')){
            var cityArea = new CityArea($(this).children('.check_box_city').attr('city'),$(this).children('.check_box_city').val());
            areaList.push(cityArea);
        }
    });
    return areaList;
}

//获取当前id下的价格
function getPriceByPriceId(priceId) {
    return $('#goods_price_'+priceId).val();
}

//构造已经设置的区域价格信息
function getAreaPriceList() {
    var areaPriceList = new Array();
    for(var i=0;i<priceIds.length;i++){
        var price = getPriceByPriceId(priceIds[i]);
        if(checkIsNull(price)){
            alertTips("价格不能为空");
            return false;
        }
        var areaList = getSelectedArea(priceIds[i]);
        if(areaList.length<1){
           alertTips("区域列表不能为空！");
           return false;
        }
        var areaPrice = new AreaPrice(price,areaList);
        areaPriceList.push(areaPrice);
    }
    return areaPriceList;
}

//构造商品对象
function Goods(goods_no,goods_name,goods_img,goods_imgs,goods_unit,goods_brand,goods_type,goods_specifications,desc,area_price_list) {
    this.goods_no = goods_no;
    this.goods_name = goods_name;
    this.goods_img = goods_img;
    this.goods_imgs = goods_imgs;
    this.goods_unit = goods_unit;
    this.goods_brand = goods_brand;
    this.goods_type = goods_type;
    this.goods_specifications = goods_specifications;
    this.desc = desc;
    this.area_price_list = area_price_list;
}

// 构造区域价格对象
function AreaPrice(price,areaList) {
    this.goods_price = price;
    this.area_list = areaList;
}

function CityArea(city,area) {
    this.city = city;
    this.area = area;
}


//判断是否存在
function checkExist(arr,name) {
    for(var i=0;i<arr.length;i++){
        if(arr[i]===name){
            return true;
        }
    }
    return false;
}

//获取时间戳
function getTimestampId() {
    return "area_"+new Date().getTime();
}




